<?php

class status_FriendshipController extends Zend_Controller_Action
{
  function init() {
  	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $this->getHelper('viewRenderer')->setNoRender();
  }







  //Create a new link between the authenticated user and someone he/she wish to follow
  function createAction() {
    require_once('status/objects/timeline.php');
    $userId =  $this->_getParam('userId');
    $userPseudo =  $this->_getParam('userPseudo');    
    if(empty($userId) && empty($userPseudo)) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	55;
      $data['error']['message'] = 'You must provide userId or userPseudo';
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;       	
    }
    
    //if the userId is empty, it means the app provided the userPseudo
    if(empty($userId)) {
    		require_once('Myzen/User.php');
		try{
			$oUser = new Myzen_User(array('pseudo' => $userPseudo));
			$userId = $oUser->getId();
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}	
    }
    
    
    try{
    	
    	$userDetails = Status_Object_Timeline::createFriendship($userId);
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }
    $data['status']= 'OK';
	$data['user'] =  $userDetails;    

 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end  create  
  
  
  
  
  //Delete existing link between the authenticated user and someone he/she wish to follow
  function deleteAction() {
    require_once('status/objects/timeline.php');
    $userId =  $this->_getParam('userId');
    $userPseudo =  $this->_getParam('userPseudo');    
    if(empty($userId) && empty($userPseudo)) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	55;
      $data['error']['message'] = 'You must provide userId or userPseudo';
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;       	
    }
    
    //if the userId is empty, it means the app provided the userPseudo
    if(empty($userId)) {
    		require_once('Myzen/User.php');
		try{
			$oUser = new Myzen_User(array('pseudo' => $userPseudo));
			$userId = $oUser->getId();
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}	
    }
    
    
    try{
    	
    	$userDetails = Status_Object_Timeline::deleteFriendship($userId);
    } catch(Status_Object_Exception $e) {
      $data['status'] = 	'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();
      echo Zend_Registry::get('dt_serializer')->serialize($data);	
      return;    	
    }
    $data['status']= 'OK';
	$data['user'] =  $userDetails;    

 	echo Zend_Registry::get('dt_serializer')->serialize($data);
    return;   	
    
      	
  } //end  create   
  
  
  
  
  
  
  
  
  //Check if a friendship exists between 2 people. It tells if the sourceUser if following or followed by the targetUser
  function viewAction() {
	//We retrieve the objects of both source and target users
	//Start with the source
	$sourceUserId = $this->_getParam('sourceUserId');
	$sourceUserPseudo = $this->_getParam('sourceUserPseudo');
	//if none are provided, we use the authenticated user data
	if(empty($sourceUserId) && empty($sourceUserPseudo)) $sourceUser = Zend_Registry::get('app_user');
	elseif(!empty($sourceUserId)) {
		require_once('Myzen/User.php');
		try{
			$sourceUser = new Myzen_User(array('id' => $sourceUserId));
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}
		
	}//end sourceUserId ! empty
	elseif(!empty($sourceUserPseudo)) {
		require_once('Myzen/User.php');

		try{
			$sourceUser = new Myzen_User(array('pseudo' => $sourceUserPseudo));
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}
		
	}//end sourceUserId ! empty	
	//Go on wiith the target with the source
	$targetUserId = $this->_getParam('targetUserId');
	$targetUserPseudo = $this->_getParam('targetUserPseudo');
	//if none are provided, we use the authenticated user data
	if(empty($targetUserId) && empty($targetUserPseudo)) {
		$data['status'] = 	'KO';
		$data['error']['num'] = 36;
		$data['error']['message'] =  'Target details must be provided';
		echo Zend_Registry::get('dt_serializer')->serialize($data);	
		return;  	
	}
	elseif(!empty($targetUserId)) {
		require_once('Myzen/User.php');
		try{
			$targetUser = new Myzen_User(array('id' => $targetUserId));
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}
		
	}//end targetUserId ! empty
	elseif(!empty($targetUserPseudo)) {
		require_once('Myzen/User.php');

		try{
			$targetUser = new Myzen_User(array('pseudo' => $targetUserPseudo));
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}
		
	}//end targetUserId ! empty		
	
	//We check that the user of source and target is not the same
	if($sourceUser->getId() == $targetUser->getId()) {
		$data['status'] = 	'KO';
		$data['error']['num'] = 	'user has probably a good friendship with himself';
		$data['error']['message'] = 	'64';	
		echo Zend_Registry::get('dt_serializer')->serialize($data);	
		return;		
	}
	
	//we call the timeline API to telle if one is following the other
	require_once('status/objects/timeline.php');
	try{
		$isSourceFollowTarget = Status_Object_Timeline::isFollowing($sourceUser, $targetUser);
		$isTargetFollowSource = Status_Object_Timeline::isFollowing($targetUser,$sourceUser);	
	} catch(Status_Object_Exception $e) {
		$data['status'] = 	'KO';
		$data['error']['num'] = 	$e->getCode();
		$data['error']['message'] = 	$e->getMessage();	
		echo Zend_Registry::get('dt_serializer')->serialize($data);	
		return;   		
	}
	//we return the 2 users and the relationships
	$data['status'] = 	'KO';
	$data['sourceUser'] = 	$sourceUser->asArray();
	$data['targetUser'] = 	$targetUser->asArray();	
	$data['sourceFollowTarget'] = 	$isSourceFollowTarget;
	$data['targetFollowSource'] = 	$isTargetFollowSource;	
	echo Zend_Registry::get('dt_serializer')->serialize($data);	
	return;   	
  } //end view
  
  
  
  
  
  
  
  
  
  //listing of followers for a certain user
  function listAction() {

	//We retrieve the objects of both source and target users
	//Start with the source
	$userId = $this->_getParam('userId');
	$userPseudo = $this->_getParam('userPseudo');
	//if none are provided, we use the authenticated user data
	if(empty($userId) && empty($userPseudo)) $user = Zend_Registry::get('app_user');
	elseif(!empty($userId)) {
		require_once('Myzen/User.php');
		try{
			$user = new Myzen_User(array('id' => $userId));
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}
		
	}//end UserId ! empty
	elseif(!empty($userPseudo)) {
		require_once('Myzen/User.php');

		try{
			$user = new Myzen_User(array('pseudo' => $userPseudo));
		} catch(Zend_Auth_Exception $e) {
			$data['status'] = 	'KO';
			$data['error']['num'] = 	$e->getCode();
			$data['error']['message'] = 	$e->getMessage();
			echo Zend_Registry::get('dt_serializer')->serialize($data);	
			return;    		
		}
		
	}//end UserId ! empty
  	
 	
	//we call the timeline API to telle if one is following the other
	require_once('status/objects/timeline.php');
    try{
    	$userDetails = Status_Object_Timeline::listFriendship($this->_getParam('mode'),$user,$this->_getParam('perPage'),$this->_getParam('page'));	  	
    } catch(Status_Object_Exception $e) {
		$data['status'] = 	'KO';
		$data['error']['num'] = 	$e->getCode();
		$data['error']['message'] = 	$e->getMessage();	
		echo Zend_Registry::get('dt_serializer')->serialize($data);	
		return;   		
	}


    $data['status']= 'OK';
    $data['user']= $user->asArray();
    $data['follow'] = Status_Object_Timeline::getConnexionNumbers($user->getId());
	$data['page'] = Status_Object_Timeline::formatPage($this->_getParam('page'));
	$data['perPage'] = Status_Object_Timeline::formatPerPage($this->_getParam('perPage'));	
	$data['mode'] = $this->_getParam('mode');
	$data['data'] =  $userDetails;

	echo Zend_Registry::get('dt_serializer')->serialize($data);	
	return;  	  	
  
  } //end list action
  
  
} //End class


?>